package com.example.demo.utils;


import com.example.demo.entity.Prize;

import java.util.*;

public class Lotteryutil_New {
    /**
     * 抽奖方法
     * create by: LH
     * @param orignalRates 商品中奖概率列表，保证顺序和实际物品对应
     * @return 中奖商品索引
     */
    public static int lottery(List<Double> orignalRates) {

        if (orignalRates == null || orignalRates.isEmpty()) {
            return -1;
        }

        int size = orignalRates.size();

        // 计算总概率，这样可以保证不一定总概率是1
        double sumRate = 0d;
        for (double rate : orignalRates) {
            sumRate += rate;
        }

        // 计算每个物品在总概率的基础下的概率情况
        List<Double> sortOrignalRates = new ArrayList<>(size);
        Double tempSumRate = 0d;
        for (double rate : orignalRates) {
            tempSumRate += rate;
            sortOrignalRates.add(tempSumRate / sumRate);
        }

        // 根据区块值来获取抽取到的物品索引
        double nextDouble = Math.random();
        sortOrignalRates.add(nextDouble);
        Collections.sort(sortOrignalRates);

        return sortOrignalRates.indexOf(nextDouble);
    }

    /**
     * 存储概率
     * @param gifts
     * @return
     */
    public static List<Double> addProbability(List<Prize> gifts){
        List<Double> orignalRates = new ArrayList<>(gifts.size());
        for (Prize gift : gifts) {
            double probability = gift.getProbability();
            Long cishu = gift.getLuckyCount();
            if (probability < 0 || cishu==0) {
                probability = 0;
            }
            orignalRates.add(probability);
        }
        return orignalRates;
    }

    /*public static void main(String[] args) {



        List<Prize> gifts = new ArrayList<>();
        gifts.add(new Prize(4,  "三等奖", "代金券10元", 0.4d,2L));
        gifts.add(new Prize(5, "四等奖","代金券2元", 0.6d,10L));
        gifts.add(new Prize(6, "五等奖","代金券1元", 0.8d,999999999L));

        // 存储概率
        *//*List<Double> orignalRates = new ArrayList<>(gifts.size());
        for (Prize gift : gifts) {
            double probability = gift.getProbability();
            Integer cishu = gift.getCishu();
            if (probability < 0 || cishu==0) {
                probability = 0;
            }

            orignalRates.add(probability);
        }*//*

        List<Double> orignalRates = addProbability(gifts);


        // 统计
        Map<Integer, Integer> count = new HashMap<>();

        // 测试次数
        double num = 1000000;
        for (int i = 0; i < num; i++) {
            int orignalIndex = lottery(orignalRates);
            Integer value = count.get(orignalIndex);
            count.put(orignalIndex, value == null ? 1 : value + 1);
            Long a=gifts.get(orignalIndex).getLuckyCount();
            int b=count.get(orignalIndex);
            if(a==b){
                gifts.get(orignalIndex).setLuckyCount(0L);
                orignalRates = addProbability(gifts);
            }
        }

        for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
            System.out.println(gifts.get(entry.getKey()) + ", 命中次数=" + entry.getValue() + ", 实际概率="
                    + entry.getValue() / num);
        }
    }*/
}
